package org.nowcoder.leetcode.bit;

import java.util.Arrays;

/**
 * Title  : 66. Plus One
 * Source : https://leetcode.cn/problems/plus-one/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-24
 */

public class LeetCode_66 {
    public int[] plusOne(int[] digits) {
        int[] result = new int[digits.length + 1];
        int bit = 1;
        for (int i = digits.length - 1; i >= 0; i--) {
            int sum = digits[i] + bit;
            bit = sum / 10;
            sum = sum % 10;
            result[i + 1] = sum;
        }
        if (bit == 1) {
            result[0] = 1;
            return result;
        } else {
            return Arrays.copyOfRange(result, 1, result.length);
        }
    }
}
